
/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')
  , http = require('http')
  , path = require('path')
  , mysql = require('mysql');

var client = mysql.createClient({
    'port':3306,
    'host':'us-cdbr-east.cleardb.com',//Testing
    'user':'b8abc1556bf3b4',//Testing
    'password':'7332a44f'//Testing
});

client.query('USE heroku_cd76840e3ca3461');//Testing

function use_database() {
    client.useDatabase("heroku_cd76840e3ca3461");
}

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser('your secret here'));
  app.use(express.session());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.enable("jsonp callback");

app.get('/', routes.index);

app.get('/renters', function(req,res){
    client.useDatabase("heroku_cd76840e3ca3461");
    client.query('SELECT * FROM Renters', function(err, results, fields){
        if(err){
            res.send({
                success: false,
                error: err
            });
            return;
        }
        res.send({
            success: true,
            renters: results
        });
    });
});

app.get('/login', function(req,res){
    res.render("login.jade");
});

app.get('/renters', function(req,res){
    client.useDatabase("heroku_cd76840e3ca3461");
    client.query('SELECT * FROM Renters', function(err, results, fields){
        if(err){
            res.send({
                success: false,
                error: err
            });
            return;
        }
        res.send({
            success: true,
            renters: results
        });
    });
});



app.get('/renters/:id', function(req,res){
    client.useDatabase("heroku_cd76840e3ca3461");
    client.query('SELECT FirstName, LastName, ID FROM Renters WHERE id=? LIMIT 1', [req.params.id], function(err, results, fields){
        var data = err?{
            success: false,
            errors: err
        }:{
            success: true,
            renters: results
        };


        res.render('profile.jade', data);

    });
});

app.post('/search', function (req, res) {
    var search = req.body.search;
    client.useDatabase("heroku_cd76840e3ca3461");
    client.query('SELECT FirstName, LastName, ID FROM renters WHERE FirstName = ?', [search], function (err, results, fields) {
        var data = err?{
            success: false,
            errors: err
        }:{
            success: true,
            renters: results
        };


        res.render('results.jade', data);
    });
});


http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});
